package com.fingard.dsp.bank.directbank.wjrb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
/**
 * @author: liuxb
 * @create: 2019-12-17 15:18
 * @version: 1.0
 **/
public class WJRB01Transaction extends WJRB01Base implements ITransaction {
    /**
     * 历史明细
     *
     * @param p_req7516
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAtsTrans = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";// 返回码
        String tmpRespInfo = "";// 返回信息
        // 写返回ATS的统一头部信息
        retAtsTrans.writeStart();
        try {
            // 设置起始时间
            //tmpClndrStepTo.get(Calendar.MONTH) MONTH是从0开始编的，意味着0是1月，11是12月
            Calendar tmpCalendarFrom = Calendar.getInstance();
            // 从报文中获取起始时间进行设置
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());
            // 设置终止时间
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                // 发送银行报文  赋值通用参数
                WJRB01Header header = new WJRB01Header();
                header.TrsCode = "B2EActTrsQry";
                header.ERPJnlNo = getQryTransID();//生成交易流水号
                header.ERPTrsTimestamp = p_req7516.reqHeader.transTime;
                //拼接请求报文头,报文体
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader2(xmlTextWriter, header);
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeStartElement("List");
                xmlTextWriter.writeStartElement("Map");
                xmlTextWriter.writeElementString("AcNo", p_req7516.actNum);//帐号
                xmlTextWriter.writeElementString("BeginDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));//查询开始日期
                xmlTextWriter.writeElementString("EndDate", Format.Date8Format.format(tmpClndrStepTo.getTime()));//查询结束日期
                xmlTextWriter.writeEndDocument();

                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), header);

                //模拟银行返回
//                String s = "<Message>\n" +
//                        "\t\t<Head>\n" +
//                        "\t\t\t<IBSReturnCode>000000</IBSReturnCode>\n" +
//                        "\t\t\t<IBSReturnMsg>银企交易成功！</IBSReturnMsg>\n" +
//                        "\t\t</Head>\n" +
//                        "<Body>\n" +
//                        "\t\t<List>\n" +
//                        "\t\t\t\t<Map>\n" +
//                        "\t\t\t\t\t<ReturnCode>000000</ReturnCode>\n" +
//                        "\t\t\t\t\t<ReturnMsg>响应1成功！</ReturnMsg>\n" +
//                        "\t\t\t\t\t<AcNo>13618800286303</AcNo>\n" +
//                        "\t\t\t\t\t\t<Map>\n" +
//                        "\t\t\t\t\t\t\t<TransDate>20120229</TransDate>\n" +
//                        "\t\t\t\t\t\t\t<TransTime>122263147423</TransTime>\n" +
//                        "\t\t\t\t\t\t\t<DCFlag>D</DCFlag>\n" +
//                        "\t\t\t\t\t\t\t<CRFlag>C</CRFlag>\n" +
//                        "\t\t\t\t\t\t\t<Amount>10.00</Amount>\n" +
//                        "\t\t\t\t\t\t\t<Currency>CNY</Currency>\n" +
//                        "\t\t\t\t\t\t\t<ChannelId>030100</ChannelId>\n" +
//                        "\t\t\t\t\t\t\t<Balance>4000</Balance>\n" +
//                        "\t\t\t\t\t\t\t<Remark>行内转账</Remark>\n" +
//                        "\t\t\t\t\t\t\t<GatheringTrsType></GatheringTrsType>\n" +
//                        "\t\t\t\t\t\t\t<GatheringTrsNo></GatheringTrsNo>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsType>001</PayTrsType>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsRefNo>090908000006</PayTrsRefNo>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsNo>090908000006</PayTrsNo>\n" +
//                        "\t\t\t\t\t\t\t<TrsFrom>0</TrsFrom>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcNo>13618800252503</PayeeAcNo>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcName>测试客户1</PayeeAcName>\n" +
//                        "\t\t\t\t\t\t\t<VoucherNo>090908000006</VoucherNo>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcBankName></PayeeAcBankName>\n" +
//                        "\t\t\t\t\t\t\t<StandbyOne></StandbyOne>\n" +
//                        "\t\t\t\t\t\t\t<StandbyTwo></StandbyTwo>\n" +
//                        "<TransJnlNo1>798050</TransJnlNo1>\n" +
//                        "\t\t\t\t\t\t\t<TransJnlNo2>1</TransJnlNo2>\n" +
//                        "\t\t\t\t\t\t</Map>\n" +
//                        "\t\t\t\t\t\t<Map>\n" +
//                        "\t\t\t\t\t\t\t<TransDate>20120216</TransDate>\n" +
//                        "\t\t\t\t\t\t\t<TransTime>122263147421</TransTime>\n" +
//                        "\t\t\t\t\t\t\t<DCFlag>D</DCFlag>\n" +
//                        "\t\t\t\t\t\t\t<CRFlag>C</CRFlag>\n" +
//                        "\t\t\t\t\t\t\t<Amount>1.10</Amount>\n" +
//                        "\t\t\t\t\t\t\t<Currency>CNY</Currency>\n" +
//                        "\t\t\t\t\t\t\t<ChannelId>030100</ChannelId>\n" +
//                        "\t\t\t\t\t\t\t<Balance>3000</Balance>\n" +
//                        "\t\t\t\t\t\t\t<Remark>行内转账</Remark>\n" +
//                        "\t\t\t\t\t\t\t<GatheringTrsType></GatheringTrsType>\n" +
//                        "\t\t\t\t\t\t\t<GatheringTrsNo></GatheringTrsNo>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsType>001</PayTrsType>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsRefNo>090908000007</PayTrsRefNo>\n" +
//                        "\t\t\t\t\t\t\t<PayTrsNo>090908000007</PayTrsNo>\n" +
//                        "\t\t\t\t\t\t\t<TrsFrom>0</TrsFrom>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcNo>13618800252503</PayeeAcNo>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcName>测试客户2</PayeeAcName>\n" +
//                        "\t\t\t\t\t\t\t<VoucherNo>090908000007</VoucherNo>\n" +
//                        "\t\t\t\t\t\t\t<PayeeAcBankName></PayeeAcBankName>\n" +
//                        "\t\t\t\t\t\t\t<StandbyOne></StandbyOne>\n" +
//                        "\t\t\t\t\t\t\t<StandbyTwo></StandbyTwo>\n" +
//                        "<TransJnlNo1>788012</TransJnlNo1>\n" +
//                        "\t\t\t\t\t\t\t<TransJnlNo2>1</TransJnlNo2>\n" +
//                        "\t\t\t\t\t\t</Map>\n" +
//                        "\t\t\t\t</Map>\n" +
//                        "\t\t</List>\n" +
//                        "\t  </Body>\n" +
//                        "</Message>\n";
//                String[] bankResult = new String[]{"", s};
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");// 响应成功
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");// 响应信息
                        if ("000000".equals(IBSReturnCode)) {
                            String returnCode = tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                            String returnMsg = tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnMsg");
                            RetAtsTransDetail detail = new RetAtsTransDetail();// 创建返回给ats的详细明细对象
                            detail.actNum = tmpBkReader.getFirstText("/Message/Body/List/Map/AcNo");//账号
                            if ("000000".equals(returnCode)) {
                                tmpRespCode = returnCode;
                                tmpRespInfo = returnMsg;
                                while (tmpBkReader.read("/Message/Body/List/Map/Map")) {
                                    XElement readXElement = tmpBkReader.readedXElement;// 创建读取返回报文的对象
                                    String tranDate = readXElement.getSingleTextNotNull("TransDate").trim();//交易日期YYYYMMDD
                                    detail.tranDate = tranDate.substring(0, 4) + "-" + tranDate.substring(4, 6) + "-" + tranDate.substring(6, 8);
                                    String times = readXElement.getSingleTextNotNull("TransTime").trim();//交易时间
                                    detail.tranTime = detail.valueDate + " " + times.substring(0, 2) + ":" + times.substring(2, 4) + ":" + times.substring(4, 6);
                                    detail.valueDate = detail.tranDate;
                                    detail.cur = convertCurToATS(readXElement.getSingleTextNotNull("Currency").trim());//币种
                                    detail.accBal = readXElement.getSingleTextNotNull("Balance").trim();//交易后余额
                                    detail.comment = readXElement.getSingleTextNotNull("Remark").trim();//摘要
                                    String cdFlag = readXElement.getSingleTextNotNull("DCFlag");//借贷标识
                                    String tranAmt = readXElement.getSingleTextNotNull("Amount");//交易额
                                    if ("D".equals(cdFlag)) {
                                        if (tranAmt.substring(0, 1).equals("-")) {
                                            detail.cdFlag = "2";
                                            detail.tranAmt = tranAmt.substring(1);
                                        } else {
                                            detail.cdFlag = "1";
                                            detail.tranAmt = tranAmt;
                                        }
                                    } else if ("C".equals(cdFlag)) {
                                        if (tranAmt.substring(0, 1).equals("-")) {
                                            detail.cdFlag = "1";
                                            detail.tranAmt = tranAmt.substring(1);
                                        } else {
                                            detail.cdFlag = "2";
                                            detail.tranAmt = tranAmt;
                                        }
                                    }
                                    retAtsTrans.writeRetDetail(detail);
                                }
                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = returnMsg;
                                break;
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {
                            tmpRespCode = "-1";
                            tmpRespInfo = IBSReturnMsg;
                            break;
                        } else {
                            retAtsTrans.writeRespCode("-1");
                            retAtsTrans.writeRespInfo("银企直联通讯机校验错误，或者通讯失败！");
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = bankResult[1];
                    break;
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception ex) {
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            ex.printStackTrace();
            WriteBankLogLn("查询明细异常:" + ex);
        }
        if ("-1".equals(tmpRespCode)) {
            retAtsTrans = new RetAtsTrans(p_req7516.reqHeader);
            retAtsTrans.writeStart();//写返回ATS的统一头部信息
            retAtsTrans.writeRespCode(tmpRespCode);
            retAtsTrans.writeRespInfo(tmpRespInfo);
        } else if (retAtsTrans.getWrittenRdCount() == 0) {
            retAtsTrans.writeRespCode("0");
            retAtsTrans.writeRespInfo("交易为空");
        }
        retAtsTrans.writeEnd();
        return retAtsTrans;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        return null;
    }
}
